package com.qingmeng.outsourcing.portal.util;

import com.qingmeng.outsourcing.model.Balance;
import com.qingmeng.outsourcing.model.User;
import com.qingmeng.outsourcing.portal.model.PortalUserDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {

    private static final Logger logger = LoggerFactory.getLogger(SecurityUtils.class);


    public static User getCurrentUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        if (authentication.getPrincipal() instanceof String) {
            logger.info("用户匿名登陆，登陆名称为" + authentication.getPrincipal());
            return null;
        }
        PortalUserDetails portalUserDetails = (PortalUserDetails) authentication.getPrincipal();
        return portalUserDetails.getUser();
    }
}
